﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.IO.IsolatedStorage;

namespace VK
{
    public partial class MainPage : PhoneApplicationPage
    {
        // айди моего приложения, зарегестрированного во вконтакте
        private const string APP_ID = "3080901";
        // тип выдаваемого вконтактом UI при авторизации
        private const string UI_TYPE = "wap";

        // Конструктор
        public MainPage()
        {
            InitializeComponent();
            Auth.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(Auth_Navigated);
            Auth.Navigate(new Uri("http://api.vkontakte.ru/oauth/authorize?client_id=" + APP_ID +"&scope=audio&redirect_uri=http://api.vkontakte.ru/blank.html&display=" + UI_TYPE + "&response_type=token"));
        }

        void Auth_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            GwtAccessTokenFromUri(Auth.Source.AbsoluteUri);
        }

        private void GwtAccessTokenFromUri(string absoluteUri)
        {
            Regex regex = new Regex(@"[a-z0-9]{50,100}");
            App._accessToken = regex.Match(absoluteUri).Value;
            
            if (App._accessToken != null)
            {
                string destination = string.Format("/Audio.xaml?access_token={0}", App._accessToken);
                NavigationService.Navigate(new Uri(destination, UriKind.Relative));
            }
        }
    }
}